/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.AddressBO;
import org.ipti.br.tag.bo.NeighborhoodBO;
import org.ipti.br.tag.bo.StreetBO;
import org.ipti.br.tag.bo.ZipcodeBO;
import org.ipti.br.tag.entity.Address;
import org.ipti.br.tag.entity.Neighborhood;
import org.ipti.br.tag.entity.Street;
import org.ipti.br.tag.entity.Zipcode;
import java.util.List;
import javax.inject.Inject;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author israel
 */
@ManagedBean(name = "testAddress")
@ViewScoped
public class TestAddressBean extends AppBaseBean{

    @Inject
    private ZipcodeBO zipcodeBO;
    @Inject
    private AddressBO address2BO;
    @Inject
    private NeighborhoodBO neighborhoodBO;
    @Inject
    private StreetBO streetBO;
    private String cep;
    private Zipcode zipcode;
    private String neighborhood;
    private String street;
    private Address address2;
    private boolean showAddress = false;

    public TestAddressBean() {
    }

    public void searchCEP() {
        zipcode = zipcodeBO.searchByCode(Integer.parseInt(cep));
        if (zipcode == null) {
            neighborhood = "";
            street = "";
            showAddress = false;
            addInfoMessage("test:cep", "CEP não cadastrado.", null);
        } else {
            neighborhood = zipcode.getNeighborhood().getName();
            street = zipcode.getStreet().getName();
            showAddress = true;
        }
        
    }

    public void saveAddress() {
        address2.setZipcode(zipcode);
        Neighborhood neighborhoodAux = neighborhoodBO.searchByNameCity(neighborhood, zipcode.getCity().getId());
        Street streetAux = streetBO.searchByNameCity(street, zipcode.getCity().getId());

        if (neighborhoodAux == null) {
            neighborhoodAux = new Neighborhood(neighborhood, zipcode.getCity());
            neighborhoodBO.create(neighborhoodAux);
        }
        if (streetAux == null) {
            streetAux = new Street(street, zipcode.getCity());
            streetBO.create(streetAux);
        }

        address2.setNeighborhood(neighborhoodAux);
        address2.setStreet(streetAux);
        address2BO.create(address2);
    }

    public List<String> neighborhoodComplete(String query) {
        return neighborhoodBO.neighborhoodList(query, zipcode.getCity());
    }

    public List<String> streetComplete(String query) {
        return streetBO.streetList(query, zipcode.getCity());
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public Zipcode getZipcode() {
        return zipcode;
    }

    public void setZipcode(Zipcode zipcode) {
        this.zipcode = zipcode;
    }

    public ZipcodeBO getZipcodeBO() {
        return zipcodeBO;
    }

    public void setZipcodeBO(ZipcodeBO zipcodeBO) {
        this.zipcodeBO = zipcodeBO;
    }

    public String getNeighborhood() {
        return neighborhood;
    }

    public void setNeighborhood(String neighborhood) {
        this.neighborhood = neighborhood;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public Address getAddress2() {
        if (address2 == null) {
            address2 = new Address();
        }
        return address2;
    }

    public void setAddress2(Address address2) {
        this.address2 = address2;
    }

    public AddressBO getAddress2BO() {
        return address2BO;
    }

    public void setAddress2BO(AddressBO address2BO) {
        this.address2BO = address2BO;
    }

    public boolean isShowAddress() {
        return showAddress;
    }

    public void setShowAddress(boolean showAddress) {
        this.showAddress = showAddress;
    }
}
